﻿package core.ui.dialogs
{
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class CurvesDialog extends BitmapOperationDialogBase
    {
        var bg:Sprite;
        var channel:Dropdown;
        var preset:Dropdown;
        var cb:Checkbox;
        var chart:Bitmap;
        var snapshot:BitmapData;
        var sampletype:ToolType;
        var curvegroup:CurveGroup;
        var chanelcurve:ChannelCurve;
        var selectedPointIndex:int = -1;
        var selectedPointIsRemoved:Boolean = false;
        var mousePoint:Point;
        var snapRadius:int = 7;
        var mouseIsOffChart:Boolean = false;
        var at:ToolButton;
        var minx:int = 0;
        var maxx:int = 256;
        var presets:Array;

        public function CurvesDialog(param1:Tuyitu)
        {
            var _loc_5:int = 0;
            this.sampletype = ToolType.None;
            this.presets = new Array();
            this.SetupSnapshot(param1);
            super(param1, "rm_curves", 325, 420, Action.CurvesAdjustment);
            this.bg = new Sprite();
            this.bg.x = 15;
            this.bg.y = 90;
            this.bg.graphics.beginFill(8421504);
            this.bg.graphics.drawRect(0, 0, 258, 258);
            this.bg.graphics.beginFill(15658734);
            this.bg.graphics.drawRect(1, 1, 256, 256);
            this.bg.graphics.endFill();
            this.bg.graphics.lineStyle(1, 11842740);
            this.bg.graphics.moveTo(64, 1);
            this.bg.graphics.lineTo(64, 259);
            this.bg.graphics.moveTo(128, 1);
            this.bg.graphics.lineTo(128, 257);
            this.bg.graphics.moveTo(193, 1);
            this.bg.graphics.lineTo(193, 257);
            this.bg.graphics.moveTo(1, 64);
            this.bg.graphics.lineTo(257, 64);
            this.bg.graphics.moveTo(1, 128);
            this.bg.graphics.lineTo(257, 128);
            this.bg.graphics.moveTo(1, 192);
            this.bg.graphics.lineTo(257, 192);
            this.bg.graphics.moveTo(1, 256);
            this.bg.graphics.lineTo(256, 1);
            addChild(this.bg);
            this.chart = new Bitmap(new BitmapData(256, 256, true, 16777215));
            this.chart.x = 1;
            this.chart.y = 1;
            this.bg.addChild(this.chart);
            var _loc_2:* = new Sprite();
            _loc_2.x = 1;
            _loc_2.y = 1;
            this.bg.addChild(_loc_2);
            if (!locked)
            {
                this.curvegroup = new CurveGroup(bm, this.chart.bitmapData, _loc_2);
                this.bg.addEventListener(MouseEvent.MOUSE_DOWN, this.MouseDown, false, 1, true);
            }
            var _loc_3:* = new Literal("rm_channel");
            _loc_3.x = 15;
            _loc_3.y = 60;
            addChild(_loc_3);
            this.channel = new Dropdown(150, 150);
            this.channel.addEventListener(SelectEvent.COMMAND, this.ChannelChange);
            this.channel.AddItem(U.RM("rm_rgb"), "0", true);
            this.channel.AddItem(U.RM("rm_red"), "1");
            this.channel.AddItem(U.RM("rm_green"), "2");
            this.channel.AddItem(U.RM("rm_blue"), "3");
            this.channel.Bind();
            this.channel.x = _loc_3.x + _loc_3.width + 5;
            this.channel.y = 60;
            addChild(this.channel);
            var _loc_4:* = new Literal("rm_presets");
            _loc_4.x = 15;
            _loc_4.y = 30;
            addChild(_loc_4);
            this.SetPresets();
            this.preset = new Dropdown(150, 150);
            this.preset.addEventListener(SelectEvent.COMMAND, this.PresetChange);
            while (_loc_5 < this.presets.length)
            {
                
                this.preset.AddItem(this.presets[_loc_5].label, _loc_5.toString(), Boolean(_loc_5 == 0));
                _loc_5++;
            }
            this.preset.Bind();
            this.preset.x = _loc_4.x + _loc_4.width + 5;
            this.preset.y = 30;
            addChild(this.preset);
            this.cb = new Checkbox("rm_histogram");
            this.cb.addEventListener(ChangeEvent.CHANGE, this.SetHistoryChange);
            this.cb.x = 15;
            this.cb.y = 360;
            addChild(this.cb);
            addChild(new ToolButton(ToolType.WhitePoint, UI.CreatePickerIcon(4294967295), 285, 90, this.ToolMouseDown, this.ToolMouseOver, param1.hover.Hide));
            addChild(new ToolButton(ToolType.GreyPoint, UI.CreatePickerIcon(4284900966), 285, 120, this.ToolMouseDown, this.ToolMouseOver, param1.hover.Hide));
            addChild(new ToolButton(ToolType.BlackPoint, UI.CreatePickerIcon(4278190080), 285, 150, this.ToolMouseDown, this.ToolMouseOver, param1.hover.Hide));
            this.UpdateCurChannelCurve();
            return;
        }// end function

        private function SetupSnapshot(flash.display:DisplayObjectContainer:Tuyitu) : void
        {
            this.snapshot = new BitmapData(flash.display:DisplayObjectContainer.stage.stageWidth < 2880 ? (flash.display:DisplayObjectContainer.stage.stageWidth) : (2880), flash.display:DisplayObjectContainer.stage.stageHeight < 2880 ? (flash.display:DisplayObjectContainer.stage.stageHeight) : (2880), false);
            this.snapshot.draw(flash.display:DisplayObjectContainer);
            flash.display:DisplayObjectContainer.modal.addEventListener(MouseEvent.ROLL_OVER, this.ShowPicker, false, 0, true);
            flash.display:DisplayObjectContainer.modal.addEventListener(MouseEvent.ROLL_OUT, this.HidePicker, false, 0, true);
            flash.display:DisplayObjectContainer.modal.addEventListener(MouseEvent.CLICK, this.PickerClick, false, 0, true);
            return;
        }// end function

        public function ToolMouseDown(event:MouseEvent) : void
        {
            if (this.at != null && this.at == event.currentTarget as ToolButton)
            {
                this.at.Mark(false);
                this.sampletype = ToolType.None;
                this.at = null;
            }
            else
            {
                if (this.at != null)
                {
                    this.at.Mark(false);
                }
                this.at = event.currentTarget as ToolButton;
                this.sampletype = this.at.type;
                this.at.Mark(true);
            }
            return;
        }// end function

        public function ToolMouseOver(event:MouseEvent) : void
        {
            switch((event.currentTarget as ToolButton).type)
            {
                case ToolType.WhitePoint:
                {
                    ed.hover.Set("rm_sample_white_point");
                    break;
                }
                case ToolType.GreyPoint:
                {
                    ed.hover.Set("rm_sample_grey_point");
                    break;
                }
                case ToolType.BlackPoint:
                {
                    ed.hover.Set("rm_sample_black_point");
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function ShowPicker(event:MouseEvent) : void
        {
            if (this.sampletype != ToolType.None)
            {
                ed.cursor.Set(ToolType.Picker);
            }
            return;
        }// end function

        private function HidePicker(event:MouseEvent) : void
        {
            if (this.sampletype != ToolType.None)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

        private function PickerClick(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:uint = 0;
            var _loc_6:Array = null;
            var _loc_7:Array = null;
            var _loc_8:Array = null;
            var _loc_9:Array = null;
            if (!locked)
            {
                if (this.sampletype != ToolType.None)
                {
                    _loc_5 = this.snapshot.getPixel32(stage.mouseX, stage.mouseY);
                    _loc_2 = _loc_5 >>> 16 & 255;
                    _loc_3 = _loc_5 >>> 8 & 255;
                    _loc_4 = _loc_5 & 255;
                    _loc_6 = new Array();
                    _loc_7 = new Array();
                    _loc_8 = new Array();
                    _loc_9 = new Array(4);
                    _loc_9[0] = [0, 255, 255, 0];
                    switch(this.sampletype)
                    {
                        case ToolType.WhitePoint:
                        {
                            _loc_6.push(0);
                            _loc_6.push(255);
                            _loc_6.push(_loc_2);
                            _loc_6.push(0);
                            _loc_9[1] = _loc_6;
                            _loc_7.push(0);
                            _loc_7.push(255);
                            _loc_7.push(_loc_3);
                            _loc_7.push(0);
                            _loc_9[2] = _loc_7;
                            _loc_8.push(0);
                            _loc_8.push(255);
                            _loc_8.push(_loc_4);
                            _loc_8.push(0);
                            _loc_9[3] = _loc_8;
                            break;
                        }
                        case ToolType.BlackPoint:
                        {
                            _loc_6.push(_loc_2);
                            _loc_6.push(255);
                            _loc_6.push(255);
                            _loc_6.push(0);
                            _loc_9[1] = _loc_6;
                            _loc_7.push(_loc_3);
                            _loc_7.push(255);
                            _loc_7.push(255);
                            _loc_7.push(0);
                            _loc_9[2] = _loc_7;
                            _loc_8.push(_loc_4);
                            _loc_8.push(255);
                            _loc_8.push(255);
                            _loc_8.push(0);
                            _loc_9[3] = _loc_8;
                            break;
                        }
                        case ToolType.GreyPoint:
                        {
                            _loc_6.push(0);
                            _loc_6.push(255);
                            _loc_6.push(_loc_2);
                            _loc_6.push(128);
                            _loc_6.push(255);
                            _loc_6.push(0);
                            _loc_9[1] = _loc_6;
                            _loc_7.push(0);
                            _loc_7.push(255);
                            _loc_7.push(_loc_3);
                            _loc_7.push(128);
                            _loc_7.push(255);
                            _loc_7.push(0);
                            _loc_9[2] = _loc_7;
                            _loc_8.push(0);
                            _loc_8.push(255);
                            _loc_8.push(_loc_4);
                            _loc_8.push(128);
                            _loc_8.push(255);
                            _loc_8.push(0);
                            _loc_9[3] = _loc_8;
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    this.channel.Set("0");
                    this.curvegroup.SetCurrentChannel(0);
                    this.curvegroup.Preset(_loc_9);
                    this.UpdateCurChannelCurve();
                    this.curvegroup.Update();
                }
            }
            return;
        }// end function

        private function SetHistoryChange(event:ChangeEvent) : void
        {
            if (!locked)
            {
                this.chart.bitmapData.fillRect(this.chart.bitmapData.rect, 16777215);
                this.curvegroup.showhistogram = this.cb.checked;
                this.curvegroup.Update();
            }
            return;
        }// end function

        private function ChannelChange(event:SelectEvent) : void
        {
            if (!locked)
            {
                this.curvegroup.SetCurrentChannel(int(event.val));
                this.UpdateCurChannelCurve();
                this.curvegroup.Update();
            }
            return;
        }// end function

        private function PresetChange(event:SelectEvent) : void
        {
            if (!locked)
            {
                this.channel.Set("0");
                this.curvegroup.SetCurrentChannel(0);
                this.curvegroup.Preset(this.presets[int(event.val)].data);
                this.UpdateCurChannelCurve();
                this.curvegroup.Update();
            }
            return;
        }// end function

        public function UpdateCurChannelCurve() : void
        {
            this.chanelcurve = this.curvegroup.channelCurves[this.curvegroup.curChannel];
            return;
        }// end function

        public function MouseDown(event:MouseEvent) : void
        {
            stage.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseMove, false, 1, true);
            this.mousePoint = new Point(this.chart.mouseX, this.chart.mouseY);
            this.selectedPointIndex = this.chanelcurve.findClosestControlPoint(this.mousePoint, this.snapRadius);
            if (this.selectedPointIndex >= 0)
            {
                this.selectedPointIsRemoved = false;
            }
            else
            {
                this.mousePoint = this.chanelcurve.findClosestLinePoint(this.mousePoint, this.snapRadius);
                if (this.mousePoint != null)
                {
                    this.chanelcurve.addPoint(this.mousePoint);
                    this.selectedPointIndex = this.chanelcurve.findClosestControlPoint(this.mousePoint, 0);
                    this.selectedPointIsRemoved = false;
                    this.curvegroup.Update();
                }
            }
            this.SetBox();
            return;
        }// end function

        private function SetBox() : void
        {
            if (this.selectedPointIndex >= 0)
            {
                if (this.selectedPointIndex == 0)
                {
                    this.maxx = this.chanelcurve.points[1].x;
                    this.minx = -1;
                }
                else if (this.selectedPointIndex == (this.chanelcurve.points.length - 1))
                {
                    this.maxx = 256;
                    this.minx = this.chanelcurve.points[this.chanelcurve.points.length - 2].x;
                }
                else
                {
                    this.maxx = this.chanelcurve.points[(this.selectedPointIndex + 1)].x;
                    this.minx = this.chanelcurve.points[(this.selectedPointIndex - 1)].x;
                }
            }
            return;
        }// end function

        private function HitTestPoint(Dropdown:Point) : Boolean
        {
            return Boolean(Dropdown.x > this.minx && Dropdown.x < this.maxx && Dropdown.y < 256 && Dropdown.y > -1);
        }// end function

        public function MouseMove(event:MouseEvent) : void
        {
            if (this.selectedPointIndex >= 0)
            {
                this.mousePoint = new Point(this.chart.mouseX, this.chart.mouseY);
                if (!this.selectedPointIsRemoved)
                {
                    if (this.HitTestPoint(this.mousePoint))
                    {
                        this.chanelcurve.movePoint(this.selectedPointIndex, this.mousePoint);
                    }
                    else if (this.selectedPointIndex > 0 && this.selectedPointIndex < (this.chanelcurve.points.length - 1))
                    {
                        this.chanelcurve.removePoint(this.selectedPointIndex);
                        this.selectedPointIsRemoved = true;
                    }
                    this.curvegroup.Update();
                }
                else if (this.HitTestPoint(this.mousePoint))
                {
                    this.chanelcurve.addPoint(this.mousePoint);
                    this.selectedPointIsRemoved = false;
                    this.curvegroup.Update();
                }
            }
            return;
        }// end function

        public function MouseUp(event:MouseEvent) : void
        {
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseMove, false);
            if (this.mouseIsOffChart)
            {
                if (!this.selectedPointIsRemoved && this.selectedPointIndex > 0 && this.selectedPointIndex < (this.chanelcurve.points.length - 1))
                {
                    this.chanelcurve.removePoint(this.selectedPointIndex);
                }
            }
            else
            {
                this.mousePoint = new Point(this.chart.mouseX, this.chart.mouseY);
                if (this.selectedPointIndex >= 0)
                {
                    if (!this.HitTestPoint(this.mousePoint))
                    {
                        if (!this.selectedPointIsRemoved && this.selectedPointIndex > 0 && this.selectedPointIndex < (this.chanelcurve.points.length - 1))
                        {
                            this.chanelcurve.removePoint(this.selectedPointIndex);
                            this.selectedPointIsRemoved = true;
                        }
                    }
                    else
                    {
                        this.chanelcurve.movePoint(this.selectedPointIndex, this.mousePoint);
                    }
                }
            }
            this.selectedPointIndex = -1;
            this.curvegroup.Update();
            return;
        }// end function

        private function SetPresets() : void
        {
            this.presets.push({label:"默认", data:[[0, 255, 255, 0]]});
            this.presets.push({label:"倒置", data:[[0, 0, 255, 255]]});
            this.presets.push({label:"冷色调", data:[[0, 255, 255, 0], [0, 255, 72, 198, 255, 0], [0, 255, 255, 0], [0, 255, 63, 184, 182, 62, 255, 0]]});
            this.presets.push({label:"暖色调", data:[[0, 255, 255, 0], [0, 255, 63, 184, 182, 62, 255, 0], [0, 255, 255, 0], [0, 255, 72, 198, 255, 0]]});
            this.presets.push({label:"增加对比度", data:[[0, 255, 64, 211, 128, 128, 192, 28, 255, 0]]});
            this.presets.push({label:"降低对比度", data:[[0, 255, 64, 175, 128, 128, 192, 81, 255, 0]]});
            this.presets.push({label:"变暗的阴影", data:[[43, 255, 94, 173, 255, 0]]});
            this.presets.push({label:"寒意汽车", data:[[0, 255, 161, 97, 255, 0], [0, 255, 166, 103, 255, 14], [0, 255, 155, 88, 240, 16], [0, 255, 120, 110, 226, 0]]});
            this.presets.push({label:"跨越进程", data:[[0, 255, 82, 153, 192, 35, 219, 13, 255, 0], [0, 255, 75, 221, 145, 123, 204, 0], [0, 255, 31, 213, 124, 131, 255, 0], [0, 255, 44, 199, 122, 143, 255, 70]]});
            this.presets.push({label:"跨过程绿色", data:[[0, 255, 255, 0], [0, 255, 62, 212, 128, 130, 174, 63, 255, 0], [0, 255, 63, 208, 93, 129, 194, 45, 255, 46], [0, 255, 52, 235, 183, 32, 255, 0]]});
            this.presets.push({label:"跨进程蓝", data:[[0, 255, 255, 0], [0, 255, 76, 208, 187, 49, 255, 0], [0, 255, 76, 206, 185, 50, 255, 0], [0, 255, 51, 188, 203, 73, 255, 0]]});
            this.presets.push({label:"跨进程红", data:[[0, 255, 255, 0], [0, 255, 17, 241, 135, 45, 184, 18, 204, 8, 255, 0], [0, 255, 46, 223, 147, 86, 193, 32, 255, 0], [0, 255, 56, 240, 208, 26, 247, 1]]});
            this.presets.push({label:"复古", data:[[0, 255, 255, 0], [0, 255, 101, 179, 223, 0], [0, 255, 90, 170, 196, 48, 255, 0], [0, 233, 255, 25]]});
            this.presets.push({label:"复古的60年代", data:[[0, 255, 255, 0], [0, 192, 255, 0], [0, 255, 255, 0], [0, 192, 255, 64]]});
            this.presets.push({label:"戴安娜", data:[[0, 255, 255, 0], [0, 255, 255, 0], [0, 255, 216, 1], [0, 255, 205, 0]]});
            this.presets.push({label:"闪光灯补", data:[[0, 255, 17, 195, 50, 141, 255, 0]]});
            this.presets.push({label:"可靠", data:[[0, 255, 255, 0], [0, 255, 156, 127, 255, 0], [0, 255, 149, 128, 255, 0], [0, 255, 82, 127, 255, 0]]});
            this.presets.push({label:"新鲜的蓝", data:[[0, 255, 91, 194, 106, 171, 255, 0], [0, 255, 79, 161, 96, 128, 255, 0], [0, 255, 74, 150, 85, 127, 255, 0], [0, 255, 37, 118, 56, 88, 255, 0]]});
            this.presets.push({label:"老古板蓝", data:[[0, 255, 106, 128, 124, 106, 255, 0], [0, 255, 103, 163, 110, 148, 255, 0], [0, 255, 85, 180, 95, 162, 255, 0], [0, 255, 83, 160, 124, 135, 255, 0]]});
            this.presets.push({label:"巧克力", data:[[0, 255, 255, 0], [0, 255, 77, 125, 255, 0], [0, 255, 148, 127, 255, 0], [0, 255, 155, 127, 255, 0]]});
            this.presets.push({label:"太阳岁", data:[[0, 255, 255, 0], [0, 233, 112, 114, 255, 53], [4, 221, 86, 121, 255, 55], [2, 207, 21, 200, 42, 172, 254, 59]]});
            this.presets.push({label:"浅琥珀色", data:[[0, 255, 255, 0], [0, 255, 92, 114, 255, 0], [0, 255, 142, 145, 255, 0], [0, 128, 255, 127]]});
            this.presets.push({label:"彩色负片", data:[[0, 255, 255, 0], [35, 1, 126, 135, 187, 254], [31, 0, 91, 158, 135, 255], [24, 1, 63, 142, 103, 255]]});
            this.presets.push({label:"宝丽来", data:[[0, 255, 255, 0], [0, 243, 21, 233, 49, 230, 91, 188, 198, 63, 255, 21], [0, 246, 21, 231, 51, 192, 90, 146, 173, 76, 255, 21], [0, 223, 52, 174, 103, 128, 189, 92, 232, 78, 255, 77]]});
            this.presets.push({label:"江山", data:[[0, 255, 97, 185, 116, 164, 255, 0], [0, 255, 78, 177, 91, 148, 255, 0], [0, 255, 105, 152, 123, 120, 255, 0], [0, 255, 93, 164, 124, 140, 255, 0]]});
            this.presets.push({label:"B＆W的镀铬", data:[[0, 255, 21, 124, 44, 18, 100, 145, 129, 255, 184, 142, 223, 65, 255, 255]]});
            return;
        }// end function

        override public function OKClick(event:MouseEvent) : void
        {
            ed.canvas.history.Commit();
            ed.canvas.UpdateAfterEdit();
            this.Remove();
            Close();
            return;
        }// end function

        override public function CloseClick(event:MouseEvent) : void
        {
            if (!locked)
            {
                bm.copyPixels(bc, bm.rect, bm.rect.topLeft);
                bc.dispose();
                bc = null;
                this.Remove();
            }
            Close();
            return;
        }// end function

        private function Remove() : void
        {
            this.bg.removeEventListener(MouseEvent.MOUSE_DOWN, this.MouseDown, false);
            ed.modal.removeEventListener(MouseEvent.ROLL_OVER, this.ShowPicker, false);
            ed.modal.removeEventListener(MouseEvent.ROLL_OUT, this.HidePicker, false);
            ed.modal.removeEventListener(MouseEvent.CLICK, this.PickerClick, false);
            return;
        }// end function

    }
}
